package com.dusk.tool
{
	import com.dusk.util.SystemUtil;
	import com.game.manager.SubWindowManager;
	
	import flash.display.NativeWindow;
	import flash.display.NativeWindowInitOptions;
	import flash.display.NativeWindowSystemChrome;
	import flash.display.NativeWindowType;
	import flash.display.Stage;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.geom.Point;
	
	public class SubWindow extends NativeWindow
	{
		
		private var closeFunc:Function = null;
		
		public function SubWindow(title:String = "",width:int = 100,height:int = 100,x:int = 100,y:int = 100)
		{
			var windowOption:NativeWindowInitOptions = new NativeWindowInitOptions();
			windowOption.type = NativeWindowType.NORMAL;
			windowOption.maximizable = false;
			windowOption.minimizable = true;
			windowOption.resizable = false;
			windowOption.systemChrome = NativeWindowSystemChrome.STANDARD;
			super(windowOption);
			this.stage.color = 0x5A5A5A;
			this.stage.align = StageAlign.TOP_LEFT;
			this.stage.scaleMode = StageScaleMode.NO_SCALE;
			this.width = width;
			this.height = height;
			this.stage.stageWidth = width;
			this.stage.stageHeight = height;
			this.title = title;
			alwaysInFront = false;
			this.addEventListener(Event.CLOSE, this.onClose);
			SubWindowManager.getIns().registerWindow(this);
		}
		
		public function setCloseFunc(func:Function):void
		{
			closeFunc = func;
		}
		
		private function onClose(evt:Event):void
		{
			while (stage.numChildren)
			{
				stage.removeChildAt(0);
			}
			SubWindowManager.getIns().removeRegisteredWindow(this);
			this.removeEventListener(Event.CLOSE, this.onClose);
			if(closeFunc)
			{
				closeFunc.call(null);
			}
		}
		
		/**
		 * 重新定位窗口
		 */
		public function relocateWindow():void
		{
			var p:Point = SystemUtil.getScreenResolution();
			var width:Number = stage.nativeWindow.width;
			var height:Number = stage.nativeWindow.height;
			stage.nativeWindow.x = ((p.x - width) * 0.5);
			stage.nativeWindow.y = ((p.y - height) * 0.5);
		}
	}
}
